package com.wondream.example.io.nio.file.simple;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

/**
 * @author wangjun
 * @note NioWriteTest
 * @date 2023-11-24  11:44
 */
public class NioReadTest {
    public static void main(String[] args) throws Exception {
        File file = new File("logs/basic.txt");
        // 1.创建输入流
        FileInputStream fis = new FileInputStream(file);
        // 2.从流中得到一个通道
        FileChannel fileChannel = fis.getChannel();
        // 3.分配缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
        // 4.读取数据到缓冲区
        fileChannel.read(byteBuffer);
        System.out.println(new String(byteBuffer.array(), StandardCharsets.UTF_8));
        // 5.关闭io
        IOUtils.closeQuietly(fis);
    }
}
